import time
import pydirectinput
from .config import Config
from .equipment import Equipment

pydirectinput.PAUSE = 0

def move_mouse():
    cur_weapon = Equipment.get_current_weapon()
    if cur_weapon.name == 'none':
        return
        
    basic = cur_weapon.basic
    speed = cur_weapon.speed
    start_time = round(time.perf_counter(), 3) * 1000

    if cur_weapon.model == 'auto':
        for i in range(cur_weapon.maxBullets):
            if not can_fire():
                break
            # 计算最终系数：倍镜系数 × 微调系数 × (按住Shift时的系数)
            hold_k = cur_weapon.hold if Config.hold else 1.0
            final_k = cur_weapon.mirror_k * cur_weapon.k * hold_k
            move_sum = int(round(basic[i] * final_k, 2))
            move_item = int(round(move_sum/speed*15, 2))
            
            while True:
                if move_sum > move_item:
                    pydirectinput.move(xOffset=0, yOffset=move_item, relative=True)
                    move_sum -= move_item
                elif move_sum > 0:
                    pydirectinput.move(xOffset=0, yOffset=move_sum, relative=True)
                    move_sum = 0
                    
                elapsed = (round(time.perf_counter(), 3) * 1000 - start_time)
                if not can_fire() or elapsed > (i+1)*speed + 10:
                    if move_sum > 0:
                        pydirectinput.move(xOffset=0, yOffset=move_sum, relative=True)
                    break
                time.sleep(0.01)

def can_fire():
    return Equipment.leftPressed and Equipment.openFlag and not Config.exitFlag

def on_click(x, y, button, pressed):
    if button.name == 'left':
        Equipment.leftPressed = pressed
        if pressed and can_fire():
            Config.pool.submit(move_mouse)
    return not Config.exitFlag

def on_release(key):
    try:
        if key.char == '1':
            Equipment.switch = 1
            final_k = Equipment.weapon1.mirror_k * Equipment.weapon1.k
            print(f'枪1:{Equipment.weapon1.name} 幅度:{Equipment.weapon1.basic} 系数:{round(final_k, 2)}')
        elif key.char == '2':
            Equipment.switch = 2
            final_k = Equipment.weapon2.mirror_k * Equipment.weapon2.k
            print(f'枪2:{Equipment.weapon2.name} 幅度:{Equipment.weapon2.basic} 系数:{round(final_k, 2)}')

        elif key.char in ['3', '4', '5']:
            Equipment.switch = 3
    except AttributeError:
        if key.name == 'num_lock':
            Equipment.openFlag = not Equipment.openFlag
        elif key.name == 'shift':
            Config.hold = False
        elif key.name == 'page_up':
            # 增加当前武器的微调系数
            if Equipment.switch == 1:
                Config.k1 += Config.k_step
                Config.k1 = round(Config.k1, 2)
                Equipment.weapon1.k = Config.k1
                final_k = Equipment.weapon1.mirror_k * Config.k1
                print(f'武器1微调系数: {Config.k1}，最终系数: {round(final_k, 2)}')
            elif Equipment.switch == 2:
                Config.k2 += Config.k_step
                Config.k2 = round(Config.k2, 2)
                Equipment.weapon2.k = Config.k2
                final_k = Equipment.weapon2.mirror_k * Config.k2
                print(f'武器2微调系数: {Config.k2}，最终系数: {round(final_k, 2)}')
        elif key.name == 'page_down':
            # 减少当前武器的微调系数
            if Equipment.switch == 1:
                Config.k1 -= Config.k_step
                Config.k1 = round(Config.k1, 2)
                Equipment.weapon1.k = Config.k1
                final_k = Equipment.weapon1.mirror_k * Config.k1
                print(f'武器1微调系数: {Config.k1}，最终系数: {round(final_k, 2)}')
            elif Equipment.switch == 2:
                Config.k2 -= Config.k_step
                Config.k2 = round(Config.k2, 2)
                Equipment.weapon2.k = Config.k2
                final_k = Equipment.weapon2.mirror_k * Config.k2
                print(f'武器2微调系数: {Config.k2}，最终系数: {round(final_k, 2)}')

def on_press(key):
    try:
        if key.name == 'shift':
            Config.hold = True
    except AttributeError:
        pass 